/*
 * $Id: QEProvinceCA.m 34 2009-04-21 20:24:55Z jon@bway.net $
 * Sleeper
 *
 * Created by Jon Gordon on 3/12/08
 * Copyright Jon Gordon.  All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#import "QEProvinceCA.h"

#import "QECountry.h"
#import "QESubdivision.h"

static NSArray *allProvinces = nil;

@implementation QEProvinceCA

+ (void)initialize
{
    static BOOL initialized = NO;
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (!initialized) {
        initialized = YES;
        // NSLog(@"  initializing class QEProvinceCA");
        qeKeyCAProvDesigProvince     = [[NSString alloc] initWithString:@"province"];
        qeKeyCAProvDesigTerritory = [[NSString alloc] initWithString:@"territory"];
        
        NSMutableArray *provinces = [NSMutableArray arrayWithCapacity:13];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Alberta" postalAbbr:@"AB" designation:qeKeyCAProvDesigProvince fips10_4:@"CA01"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"British Columbia" postalAbbr:@"BC" designation:qeKeyCAProvDesigProvince fips10_4:@"CA02"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Manitoba" postalAbbr:@"MB" designation:qeKeyCAProvDesigProvince fips10_4:@"CA03"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"New Brunswick" postalAbbr:@"NB" designation:qeKeyCAProvDesigProvince fips10_4:@"CA04"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Newfoundland and Labrador" postalAbbr:@"NL" designation:qeKeyCAProvDesigProvince fips10_4:@"CA05"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Nova Scotia" postalAbbr:@"NS" designation:qeKeyCAProvDesigProvince fips10_4:@"CA07"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Ontario" postalAbbr:@"ON" designation:qeKeyCAProvDesigProvince fips10_4:@"CA08"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Prince Edward Island" postalAbbr:@"PE" designation:qeKeyCAProvDesigProvince fips10_4:@"CA09"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Quebec" postalAbbr:@"QC" designation:qeKeyCAProvDesigProvince fips10_4:@"CA10"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Saskatchewan" postalAbbr:@"SK" designation:qeKeyCAProvDesigProvince fips10_4:@"CA11"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Yukon" postalAbbr:@"YT" designation:qeKeyCAProvDesigTerritory fips10_4:@"CA12"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Northwest Territories" postalAbbr:@"NT" designation:qeKeyCAProvDesigTerritory fips10_4:@"CA13"] autorelease]];
        [provinces addObject:[[[QEProvinceCA alloc]
                               initWithName:@"Nunavut" postalAbbr:@"NU" designation:qeKeyCAProvDesigTerritory fips10_4:@"CA14"] autorelease]];
        
        allProvinces = [[NSArray alloc] initWithArray:provinces];
    } // if
} // initialize


+ (NSArray *)allProvinces
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return allProvinces;
} // allProvinces


- (id)init
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [self initWithName:nil postalAbbr:nil designation:nil fips10_4:nil iso3166_2:nil];
} // init


- (id)initWithName:(NSString *)newName
        postalAbbr:(NSString *)newPostalAbbr
       designation:(NSString *)newDesignation
          fips10_4:(NSString *)newFips10_4
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [self initWithName:newName 
                   postalAbbr:newPostalAbbr 
                  designation:newDesignation 
                     fips10_4:newFips10_4 
                    iso3166_2:[NSString stringWithFormat:@"CA-%@", newPostalAbbr]];
} // initWithName:postalAbbr:designation:fips10_4:


- (id)initWithName:(NSString *)newName
        postalAbbr:(NSString *)newPostalAbbr
       designation:(NSString *)newDesignation
          fips10_4:(NSString *)newFips10_4
         iso3166_2:(NSString *)newIso3166_2
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [super initWithName:newName
                   designation:newDesignation
                     iso3166_2:newIso3166_2 
                          fips:newFips10_4
                    postalAbbr:newPostalAbbr
                    divisionOf:[QECountry countryForName:@"Canada"]];
} // initWithName:postalAbbr:designation:fips10_4:iso3166_2:

- (NSString *)fips10_4
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [super fips];
} // fips10_4

- (void)dealloc
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    [postalAbbr release];
    
    [super dealloc];
} // dealloc

- (id)copyWithZone:(NSZone *)zone
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    QEProvinceCA *newData = [[[self class] allocWithZone:zone]
                             initWithName:[self name] 
                             postalAbbr:[self postalAbbr] 
                             designation:[self designation] 
                             fips10_4:[self fips10_4] 
                             iso3166_2:[self iso3166_2]];
    return newData;
} // copyWithZone:

- (unsigned int)hash
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    static const unsigned int HASH_FACTOR = 37;
    
    unsigned int result = [super hash];
    result = (result * HASH_FACTOR) + [[self postalAbbr] hash];
    
    return result;
} // hash

- (BOOL)isEqual:(id)anObject
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return ([super isEqual:anObject] &&
            [[self postalAbbr] isEqual:[anObject postalAbbr]]);
} // isEqual:
@end
